<!-- 1903010328 查询通行证 -->
<?php
$loginPath = "../login.html";
include "../inc/log/session.php";
include "../inc/data/conn.php";
?>
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>录入成绩</title>
</head>
<body>
	<?php
	// 1903010328 添加组件信息
	require "../vendor/autoload.php";
	use PhpOffice\PhpSpreadsheet\IOFactory;
	// 1903010328 实例化对象，读取本地文件
	$file = $_FILES["scorefile"]["tmp_name"];
	$spreadSheet = IOFactory::load($file);
	// 1903010328 获取表
	$workSheet = $spreadSheet->getSheet(0);
	$row = $workSheet->getHighestRow();
	// 1903010328 获取动态信息taskid
	$taskId = $_POST["taskid"];
	// 1903010328 插入数据
	
	for ($i = 2; $i <= $row; $i++) {
		$sql = "insert into mamx_score
				(taskid,stuid,scfinal,scoverall)
				values
				($taskId,?,?,?)";
		if ($stmt=$conn->prepare($sql)){
		// 1903010328 获取学号列的值，并进行判断
		$stuId = $workSheet->getCell("a$i")->getValue();
		// 1903010328 
			// 1903010328 获取学生成绩
			$scFinal = $workSheet ->getCell("g$i")->getValue();
			$scOverall = $workSheet ->getCell("g$i")->getValue();
			$stmt->bind_param("idd",$stuId,$scFinal,$scOverall);
			$stmt->execute();
			if($stmt->affected_rows ==1 ) {
				$count += 1;
			}
		}
	}
	if ($count > 0){
		// 1903010328 录入成功
		echo <<<END
	    <script type="text/javascript">
	    alert(" $count 位学生的成绩已成功录入！");
	    history.back();
	    </script>
END;
	}else{
		// 1903010328 录入失败
		echo <<<END
			<script type="text/javascript">
			alert("录入失败，请重新尝试！");
			location="tScoreln.php";
			</script>
END;
	}
	?>
</body>
</html>